package mySql;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class publish extends HttpServlet {


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=gb2312");
		PrintWriter out = response.getWriter();
		doPost(request, response);
		out.close();
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=gb2312");
		PrintWriter out = response.getWriter();
		
		String login_name="root";
		String login_password="2014121167";
		String driverClass="com.mysql.jdbc.Driver";
		String url="jdbc:mysql://localhost:3306/test";
		Connection con=null;
		PreparedStatement pst=null;
		int rs;
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("UTF-8");
		
		

		DBConnection dbcon=new DBConnection(driverClass,url,login_password,login_name);
		try
		{
			if(request.getParameter("publish_img").equals("")||request.getParameter("publish_name").equals("")||request.getParameter("publish_price").equals(""))
			{
			response.setHeader("refresh", "1;URL="+"http://localhost:8080/practise/jsp/manage_publish.jsp");
			out.print("请每项都要填写!");
			}
			else
			{
				con=dbcon.getConnection();
				pst=con.prepareStatement("insert into goods_detail values(?,?,?)");
				pst.setString(1,"image/"+request.getParameter("publish_img"));
				pst.setString(2, request.getParameter("publish_name"));
				pst.setString(3, request.getParameter("publish_price"));
				
				rs=pst.executeUpdate();
				response.setHeader("refresh", "1;URL="+"http://localhost:8080/practise/jsp/manage_publish.jsp");
				out.print("发布成功!");
			}
		}
		catch (Exception e) {
			e.getStackTrace();
			e.printStackTrace();
		}
		
		
		
		
		out.close();
	}

}
